package uk.ac.glasgow.demosocs.tests;

import static org.junit.Assert.*;

import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.ac.glasgow.demosocs.VotingSystem;
import uk.ac.glasgow.demosocs.impl.VotingSystemStub;

public class Nominate {
	
	private VotingSystem testVS;

	@Before
	public void setUp() throws Exception {
		testVS = new VotingSystemStub();
		
		testVS.setCurrentUser("admin", "p");
		testVS.createElection("election", "Simple_Plurality");
		testVS.editElection(new Date(), new Date(), new Date(), "Level_3");
	}

	@After
	public void tearDown() throws Exception {
		testVS = null;
	}
	@Test
	/** 
	 * tests getNominations by nominating candidates,
	 * and checking that the set of nominated candidates
	 * is the correct size.
	 */
	public void selfNominationTest() {

		String ID = testVS.getCurrentElection().getEID();

		Date startD = new Date();
		startD.setTime(startD.getTime() + 1000000000000000000L);
		testVS.editElection(new Date(), startD, startD, "Level_3");

		testVS.setCurrentElection(ID);
		
		int sfirst = testVS.getNominations().size();

		testVS.nominateCandidate("3");
	
		int s = testVS.getNominations().size();

		assertEquals(0, s-sfirst);
	}
	
	@Test
	/**
	 * nominating valid nominee on electorate
	 */
	public void validNominationTest() {

		String ID = testVS.getCurrentElection().getEID();
		
		Date startD = new Date();
		startD.setTime(startD.getTime() + 1000000000000000000L);
		testVS.editElection(new Date(), startD, startD, "Level_3");

		testVS.setCurrentElection(ID);

		testVS.setCurrentUser("2", "p");
		int sfirst = testVS.getNominations().size();

		testVS.setCurrentUser("1", "p");
		testVS.nominateCandidate("2");
		
		testVS.setCurrentUser("2", "p");
	
		int s = testVS.getNominations().size();

		assertEquals(1, s-sfirst);
	}
//	
//	@Test
//	/**
//	 * nominating valid nominee on electorate by non-admin user
//	 */
//	public void validUserNominationTest() {
//
//		String ID = testVS.getCurrentElection().getEID();
//		
//		Date startD = new Date();
//		startD.setTime(startD.getTime() + 1000000000000000000L);
//		testVS.editElection(new Date(), startD, startD, "Level_3");
//
//		testVS.setCurrentElection(ID);
//		
//		testVS.setCurrentUser("2", "p");
//		int sfirst = testVS.getNominations().size();
//
//		testVS.setCurrentUser("1", "p");
//		testVS.nominateCandidate("2");
//		
//		testVS.setCurrentUser("2", "p");
//		int s = testVS.getNominations().size();
//
//		assertEquals(1, s-sfirst);
//	}
//	
	
	@Test
	/**
	 * nominating invalid nominee not on electorate
	 */
	public void invalidNomineeTest() {

		String ID = testVS.getCurrentElection().getEID();
		
		Date startD = new Date();
		startD.setTime(startD.getTime() + 1000000000000000000L);
		testVS.editElection(new Date(), startD, startD, "Level_3");
		testVS.setCurrentElection(ID);
		
		testVS.setCurrentUser("5", "p");
		int sfirst = testVS.getNominations().size();

		testVS.setCurrentUser("3", "p");
		testVS.nominateCandidate("5");
		
		testVS.setCurrentUser("5", "p");
		int s = testVS.getNominations().size();

		assertEquals(0, s-sfirst);
	}

	@Test
	/**
	 * nominating before nominations open
	 */
	public void nomBeforeNomsOpenTest() {

		String ID = testVS.getCurrentElection().getEID();
		
		Date startD = new Date();
		startD.setTime(startD.getTime() + 1000000000000000000L);
		testVS.editElection(startD, startD, startD, "Level_3");

		testVS.setCurrentElection(ID);
		
		testVS.setCurrentUser("1", "p");
		int sfirst = testVS.getNominations().size();

		testVS.setCurrentUser("3", "p");
		testVS.nominateCandidate("1");
		
		testVS.setCurrentUser("1", "p");
	
		int s = testVS.getNominations().size();

		assertEquals(0, s-sfirst);
	}
	
	@Test
	/**
	 * nominating after nominations close (voting)
	 */
	public void nomAfterNomsCloseTest() {

		String ID = testVS.getCurrentElection().getEID();

		Date startD = new Date();
		startD.setTime(startD.getTime() - 1000000000000000000L);
		testVS.editElection(startD, startD, new Date(), "Level_3");

		testVS.setCurrentElection(ID);
		
		testVS.setCurrentUser("1", "p");
		int sfirst = testVS.getNominations().size();

		testVS.setCurrentUser("3", "p");
		testVS.nominateCandidate("1");
		
		testVS.setCurrentUser("1", "p");
	
		int s = testVS.getNominations().size();

		assertEquals(0, s-sfirst);
	}
	
	@Test
	/**
	 * nominating after election closed
	 */
	public void nomAfterElectionClosedTest() {

		String ID = testVS.getCurrentElection().getEID();

		Date startD = new Date();
		startD.setTime(startD.getTime() - 1000000000000000000L);
		testVS.editElection(startD, startD, startD, "Level_3");

		testVS.setCurrentElection(ID);
		
		testVS.setCurrentUser("1", "p");
		int sfirst = testVS.getNominations().size();

		testVS.setCurrentUser("3", "p");
		testVS.nominateCandidate("1");
		
		testVS.setCurrentUser("1", "p");
	
		int s = testVS.getNominations().size();

		assertEquals(0, s-sfirst);
	}
}
